<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 最大交换 -->
    <script>
      let maximumSwap = function (num) {
        let arr = String(num).split('')
        let temp = arr.slice().sort((a, b) => b - a)
        let flag = -1
        for (let i = 0; i < arr.length; i++) {
          if (arr[i] < temp[i]) {
            flag = i
            break
          }
        }
        if (flag < 0) {
          return num
        }
        for (let i = arr.length - 1; i > flag; i--) {
          if (arr[i] === temp[flag]) {
            ;[arr[i], arr[flag]] = [arr[flag], arr[i]]
            break
          }
        }
        return parseInt(arr.join(''))
      }
      console.log(maximumSwap(2736))
    </script>
  </body>
</html>
